from .api_token import get_id,get_time,get_token
from util.time_util import timestran
import requests
import json
with open(r'data\json_data\homepage.json','r',encoding='utf-8') as f:
    data = json.load(f)['api']
#获取顶部统计数据
def get_toplist():
    values =[]
    url = 'http://hxzhan.rtcloud.com:32111/tm/home/overview'
    headers = {
        'content-type':'application/json; charset=utf-8',
        'Tm-Token':get_token(), 
        'rt-request-ts':get_time(),
        'rt-request-id':get_id()
    }
    response = requests.post(url=url,headers=headers)
    for key in data['count_key']:
        values.append(response.json()['data'][key])
    return values
#获取个人中心数据
def get_personcenter():
    values =[]
    url = 'http://hxzhan.rtcloud.com:32111/tm/roles/user/info'
    headers = {
        'content-type':'application/json; charset=utf-8',
        'Tm-Token':get_token(), 
        'rt-request-ts':get_time(),
        'rt-request-id':get_id()
    }
    response = requests.get(url=url,headers=headers)
    for key in data['key_personcenter']:
        if key == 'gender':
            if response.json()['data'][key] == 1:
                values.append('男')
            else:
                values.append('女')
        elif key == 'ctrl_role_list' or key == 'data_role_list':
            for name in response.json()['data'][key]:
                values.append(name['roleName'])
        else:
            values.append(response.json()['data'][key])
    return values
#获取告警列表数据
def get_alarlist(scene_id=0):
    values = []
    url = ' http://hxzhan.rtcloud.com:32111/tm/alarm/list'
    headers = {
        'content-type':'application/json; charset=utf-8',
        'Tm-Token':get_token(), 
        'rt-request-ts':get_time(),
        'rt-request-id':get_id()
    }
    json = {'page': 1, 'limit': 6,'scene_id':scene_id}
    response = requests.post(url=url,headers=headers,json=json)
    for elm in response.json()['data']['list']:
        for key in data['alarm_key']:
            if key == 'alarm_at':
                values.append(timestran(elm[key]))
            elif key == 'level':
                if elm[key] == 1:
                    values.append(data['alarm_level'][0])
                elif elm[key] == 2:
                    values.append(data['alarm_level'][1])
            elif key == 'return_status':
                if elm[key] == 2:
                    values.append('未复归')
                elif elm[key] == 1:
                    values.append('已复归')
            elif key == 'app_scen_name':
                if scene_id == 137:
                    continue
                else:
                    values.append(elm[key])   
            else:
                values.append(elm[key])   
    return values
#获取首页站点数据
def get_sitelist(name):
    values = []
    numbers = []
    for status in range(0,4):
        url = 'http://hxzhan.rtcloud.com:32111/tm/home/station/overview'
        headers = {
            'content-type':'application/json; charset=utf-8',
            'Tm-Token':get_token(), 
            'rt-request-ts':get_time(),
            'rt-request-id':get_id()
        }
        json = {'page': 1, 'limit': 100,'status':status}
        response = requests.post(url=url,headers=headers,json=json)
        sitelist = response.json()['data']['list']
        for site in sitelist:
                for key in data['site_key']:
                    if key == 'gateway_online_status':
                        if site[key] == 1:
                            values.append('网关状态：在线')
                        elif site[key] == 2:
                            values.append('网关状态：离线')
                        elif site[key] == 3:
                            values.append('网关状态：部分离线')
                    else:
                        values.append(str(site[key]))
    for key in data['key_number']:
        numbers.append(response.json()['data']['nums'][key])
    if name == 'number':
        return numbers
    else:
        return values
